import { addMonths, format, subMonths } from 'date-fns';

//获取年月
const getDate = (date: Date) => {
  return format(date, 'yyyy-MM');
};
/*下一个月*/
const getNextMonth = () => {
  return format(addMonths(new Date(), 1), 'yyyy-MM');
};
/*上一个月*/
const getPreviousMonth = () => {
  return format(subMonths(new Date(), 1), 'yyyy-MM');
};
/*当前年月*/
const getNow = () => {
  return format(new Date(), 'yyyy-MM');
};
/*当前年*/
export const getYear = () => {
  return format(new Date(), 'yyyy');
};

export const formatDateTime = (date: Date): string => {
  const pad = (num: number) => num.toString().padStart(2, '0');
  return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`;
};

export default { getDate, getNextMonth, getPreviousMonth, getNow, formatDateTime };
